package com.srwl.mytx.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.DialogFragment;

import com.srwl.mytx.domain.Contact;
import com.srwl.mytx.Helper;
import com.srwl.mytx.R;

import java.util.ArrayList;
import java.util.List;


public class GroupFragment extends DialogFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;
    private ListView listView;
    private EditText et_creatGroup;
    private RelativeLayout rl_confirm;
    private List<String> orignalGroupList;
    private View ll_selectGroup;

    public GroupFragment() {
        // Required empty public constructor
    }


    public static GroupFragment newInstance() {
        GroupFragment fragment = new GroupFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo_Light_Dialog_MinWidth);
        initGroupData();

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.group_dialog, container, false);
        rl_confirm = view.findViewById(R.id.rl_confirm);
        view.findViewById(R.id.rl_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        et_creatGroup = view.findViewById(R.id.et_create_group);
        listView = view.findViewById(R.id.list_group);
        ll_selectGroup = view.findViewById(R.id.ll_select_group);
        initListView();
        initClickListener();
        return view;
    }

//    @NonNull
//    @Override
//    public Dialog onCreateDialog(Bundle savedInstanceState) {
//        Dialog dialog = new Dialog(getActivity());
//        //dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // must be called before set content
//        dialog.setTitle("设置分组");
//        dialog.setContentView(R.layout.bottom_dialog);
//        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLACK));
//        dialog.setCanceledOnTouchOutside(true);
//        return  dialog;
//    }

    private void initClickListener() {

        rl_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String groupName = et_creatGroup.getText().toString().trim();
                if (TextUtils.isEmpty(groupName)) {
                   // Snackbar.make(rl_confirm, "分组名称不能为空", Snackbar.LENGTH_LONG).show();
                   // rl_confirm.setEnabled(true);
                 //   return;
                    groupName = et_creatGroup.getHint().toString();
                }

                if (mListener != null) {
                    mListener.onCreateOrSelectGroup(groupName);
                }
            }
        });
    }

    private void initGroupData() {
        orignalGroupList = new ArrayList<>();
        List<Contact> contactList = Helper.getInstance().getContactList();
        for (Contact contact : contactList) {
            if (!orignalGroupList.contains(contact.getGroupName())) {
                orignalGroupList.add(contact.getGroupName());
            }
        }
    }

    private void initListView() {
        if (orignalGroupList.size() == 0) {
            ll_selectGroup.setVisibility(View.GONE);
            return;
        }
        listView.setAdapter(new MyBaseAdapter(orignalGroupList));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                if (mListener != null) {
                    mListener.onCreateOrSelectGroup(orignalGroupList.get(position));
                }

            }
        });
    }


    private class MyBaseAdapter extends BaseAdapter {
        private List<String> originalGroupList;

        public MyBaseAdapter(List<String> orignalGroupList) {
            this.originalGroupList = orignalGroupList;
        }

        @Override
        public int getCount() {

            return originalGroupList.size();
        }

        @Override
        public Object getItem(int position) {
            return originalGroupList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.tx_list_item_group, null);
            }
            TextView tv = convertView.findViewById(R.id.tv_group_item);

            tv.setText(originalGroupList.get(position));
            return convertView;
        }
    }

    public void setInteractionListener(OnFragmentInteractionListener listener) {
        mListener = listener;
    }

    public interface OnFragmentInteractionListener {

        void onCreateOrSelectGroup(String groupName);
    }
}
